﻿using _06_坦克大战_正式.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _06_坦克大战_正式
{
    internal class MyTank:Movething
    {
        private bool isMoving = false;
        public int HP { get; set; }
        private int originalx;
        private int originaly;
        public MyTank(int x,int y,int speed)
        {
            isMoving = false;
            this.X = x;
            this.Y = y;
            originalx = x;
            originaly = y;
            this.Speed = speed; 
            BitmapDown = Resources.MyTankDown;
            BitmapLeft = Resources.MyTankLeft;
            BitmapUp = Resources.MyTankUp;
            BitmapRight = Resources.MyTankRight;
            this.Dir = Direction.Up;
            HP = 4;
        }
        private void Move()
        {
            if (!isMoving) return;
            switch (Dir)
            {
                case Direction.Up:
                    Y -= Speed;
                    break;
                case Direction.Down:
                    Y += Speed;
                    break;
                case Direction.Left:
                    X -= Speed;
                    break;
                case Direction.Right:
                    X += Speed;
                    break;
                default:
                    break;
            }


        }
        private void MoveCheck()
        {

            #region 边界碰撞检测
            //检查窗体边界
            if (Dir == Direction.Up)
            {
                if (Y - Speed < 0)
                {
                    isMoving = false;
                    return;
                }
            }
            else if (Dir == Direction.Down)
            {
                if (Y + Speed + Height > 450)
                {
                    isMoving = false;
                    return;
                }
            }
            else if (Dir == Direction.Left)
            {
                if (X - Speed < 0)
                {
                    isMoving = false;
                    return;
                }
            }
            else if (Dir == Direction.Right)
            {
                if (X + Height + Speed > 450)
                {
                    isMoving = false;
                    return;
                }
            }
            #endregion

            //检查墙体
            Rectangle rect = GetRectangle();
            switch (Dir)
            {
                case Direction.Up:
                    rect.Y -= Speed;
                    break;
                case Direction.Down:
                    rect.Y += Speed;
                    break;
                case Direction.Left:
                    rect.X -= Speed;
                    break;
                case Direction.Right:
                    rect.X += Speed;
                    break;
            }
            if (GameObjectManager.IsColliedeWall(rect) != null)
            {
                isMoving = false;
                return;
            }
            else if (GameObjectManager.IsColliedeSteel(rect) != null)
            {
                isMoving = false;
                return;
            }
            else if (GameObjectManager.IsColliedeBoss(rect))
            {
                isMoving = false;
                return;
            }

        }
        public override void Update()
        {
            MoveCheck();
            Move();
            base.Update();
        }


        public void KeyDown(KeyEventArgs args)
        {
            switch (args.KeyCode)
            {
                case Keys.W:
                    if (Dir != Direction.Up)
                        Dir = Direction.Up;
                    isMoving = true;
                    break;
                case Keys.S:
                    if (Dir != Direction.Down)
                        Dir = Direction.Down;
                    isMoving = true;
                    break;
                case Keys.A:
                    if (Dir != Direction.Left)
                        Dir = Direction.Left;
                    isMoving = true;
                    break;
                case Keys.D:
                    if (Dir != Direction.Right)
                        Dir = Direction.Right;
                    isMoving = true;
                    break;
                case Keys.J:
                    attack();
                    break;
                default:
                    break;
            }
        }
        private void attack()
        {
            SoundManager.Playfire();
            int x = this.X;
            int y = this.Y;
            switch (Dir)
            {
                case Direction.Up:
                        x = x + Wideth / 2;
                    break;
                case Direction.Down:
                    x = x + Wideth / 2;
                    y = y + Height;
                    break;
                case Direction.Left:
                    y = y + Height / 2;
                    break;
                case Direction.Right:
                    x = x + Wideth;
                    y = y + Height / 2;
                    break;
                default:
                    break;
            }
            GameObjectManager.CreateBullet(x,y,Tag.MyTank,Dir);
        }
        public void KeyUp(KeyEventArgs args)
        {
            switch (args.KeyCode)
            {
                case Keys.W:
                    isMoving = false;
                    break;
                case Keys.S:
                    isMoving = false;
                    break;
                case Keys.A:
                    isMoving = false;
                    break;
                case Keys.D:
                    isMoving = false;
                    break;
                default:
                    break;
            }
        }
        public void TakeDamage()
        {
            HP--;
            if(HP < 0)
            {
                X = originalx;
                Y = originaly;
                HP = 4;
            }
        }

    }
}
